home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15695 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  84 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What are the differences between structures and classes in C++ ?
  5. Date: 7 Apr 1996 21:41:55 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4k9cr3$fl9@arl-news-svc-5.compuserve.com>
  8. NNTP-Posting-Host: ad04-110.compuserve.com
  9.  
  10. marnold@netcom.com (Matt Arnold) s'Θcrit :
  11. > Raghuveera Ravinutala <raghur> writes:
  12. > ><jtbell@presby.edu (Jon Bell)> wrote :
  13. > >>the only *formal* difference between a class and a
  14. > >>struct is that by default, class members are private whereas struct
  15. > >>members are public.
  16. > >Thanx. I am aware of this difference. I would like to know more, like,
  17. > >inheritence etc..
  18. > There is none, other the default protection state of members.  This is
  19. > what the original poster meant by only a formal difference.  All the
  20. > rules you know about classes in C++ also apply to structs.  For example,
  21. > the following two versions of Foo are precisely equivalent...
  22. > // version #1, using class keyword
  23. > class Foo: public Bar
  24. >    {
  25. >    public:
  26. >       // some public members
  27. >    private:
  28. >       // some private members
  29. >    };
  30. > // version #2, using struct keyword
  31. > struct Foo: public Bar
  32. >    {
  33. >    public:
  34. >       // some public members
  35. >    private:
  36. >       // some private members
  37. >    };
  38. > ...since the protection of all members is explicit.  And, the fact that 
  39. > Foo is a struct or class doesn't affect the inheritance from Bar (that
  40. > is, whatever public interface Bar defines becomes part of Foo's, in 
  41. > either case, as expected).
  42. > Structs can inherit from other structs and classes and can be inherited
  43. > by other structs and classes.  Structs can have virtual functions, member 
  44. > operators, constructors and destructors, you can create template structs,
  45. > and so on.
  46. > In other words, structs *are* classes.
  47.  
  48. And so, the "class" keyword was not necessary to C++ !!! It is
  49. for only an historical (and cultural) reason that classes in C
  50. can be specified with the "class" keyword. Historically,
  51. structs in first C++ beta versions did not allow for methods
  52. and constructor (because classes complicated the C++ to C
  53. conversion, and made compilation time and generated volume
  54. of C source much bigger). However, the need of costructors
  55. for PODS structs relaxed this rule, and structs gained all
  56. the capabilities of classes, for clarity and simplicity !
  57.  
  58. In most sources,
  59. the public members are specified at the beginning of the class
  60. definition so using "struct" instead of "class" avoids to
  61. explicitly use the "public:" access modifier and saves 7
  62. characters !!!
  63.  
  64. > Regards,
  65. > -------------------------------------------------------------------------
  66. > Matt Arnold                       |        | ||| | |||| |  | | || ||
  67. > marnold@netcom.com                |        | ||| | |||| |  | | || ||
  68. > Boston, MA                        |      0 | ||| | |||| |  | | || ||
  69. > 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  70. > C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  71. > -------------------------------------------------------------------------
  72.  
  73.